$(function () {
    setSignInData();
})
//为当前显示条数文本框绑定回车事件
$('#size').bind('keypress', function (event) {
    if (event.keyCode == "13") {
        //敲击回车后重新按照新的显示条数加载页面
        setSignInData();
    }
});

//为当前页数文本框绑定回车事件
$('#pageipt').bind('keypress', function (event) {
    if (event.keyCode == "13") {
        setSignInData();
    }
});
var total = 0;
var index = 1;
var pageSize = 5;
//为上一页绑定点击事件
$('#lastpage').click(function () {
    //获取默认的当前页数和显示条数值
    let page = $("#pageipt").val();
    page--;
    if (page >= 1) {//如果不是第一页，则往前翻一页
        $("#pageipt").val(page);
        setSignInData();
    }
});

//绑定下一页点击事件
$('#nextpage').click(function () {
    //获取默认的当前页数和显示条数值
    let page = $("#pageipt").val();
    let size = $("#size").val();
    page++;
    if (page <= Math.ceil(total / size)) {
        $("#pageipt").val(page);
        setSignInData();
    }
});

//为首页绑定点击事件
$("#first").click(function () {
    let page = $("#pageipt").val();
    if (page > 1) {//如果当前页不是首页则跳转到首页，否则不做任何事情
        page = 1;
        $("#pageipt").val(page);
        setSignInData();
    }
});

//为尾页绑定点击事件
$("#last").click(function () {
    let page = $("#pageipt").val();
    let size = $("#size").val();
    if (page < Math.ceil(total / size)) {//如果当前页不是尾页，则跳转到尾页
        page = Math.ceil(total / size);
        $("#pageipt").val(page);
        setSignInData();
    }
});

//显示签到管理页数据
function setSignInData() {
    //获取登录用户的编号
    let leader = sessionStorage.getItem("username");
    //根据用户编号获取该用户是否为组长
    let data = myAjax("/team/getTeamByLeader", "get", {"leader": leader}, "json");
    $("tbody").html("");
    //如果该用户是该小组组长
    if (data.code == 200) {
        let team = data.model["team"];
        $("#team_name").html(team.name);
        $("#team_leader").html(team.leader.name);
        $("#team_code").html(team.code);
        //展示组内成员信息
        setTeamMember(JSON.stringify(team.code));
    } else {
        alert("您没有该权限");
        $(".table").html("");
    }
}
//设置小组成员
function setTeamMember(code) {
    console.log(JSON.parse(code));
    //获取小组成员
    let member = myAjax("/user/getUserByTeam", "get", {"team": JSON.parse(code)}, "json");
    console.log(member);
    sessionStorage.setItem("team_member", JSON.stringify(member));
    $(".modal-content").html("");
    $.each(member, function (i, user) {
        $("tbody").append("<tr><td><input type='checkbox'></td><td>" + user.id + "</td><td>" + user.username + "</td>" +
            "<td>" + avoidNull(user.name) + "</td><td>" + avoidNull(user.gender.name) + "</td><td>" + avoidNull(user.age) + "</td><td>" + (user.team == null ? "暂未分组" : user.team.name) + "</td>" +
            "<td>" + avoidNull(user.degree.name) + "</td><td>" + avoidNull(user.graduate) + "</td><td>" + avoidNull(user.nativePlace) + "</td>" +
            "<td>" + avoidNull(user.address) + "</td><td>" + avoidNull(user.phone) + "</td><td>" + avoidNull(user.emergencyPhone) + "</td>" +
            "<td>" + user.createTime + "</td><td>" + (user.updateTime == null ? "暂无更新" : user.updateTime) + "</td><td>" + user.status.name + "</td></tr>");
        let flag = getSignByUserAndSignTime(JSON.stringify(user.username));
        if (flag) {
            $(".modal-content").append("<div><label class='user_name'><input hidden='hidden' value='" + user.username + "'>" + user.name + "</label>:" +
                "<label><label><input type='radio' name='state_" + user.username + "' value='fine' onclick='hideLaterTime(" + JSON.stringify(user.username) + ")' checked='checked'>正常</label>" +
                "<label><input type='radio' name='state_" + user.username + "' value='later' onclick='showLaterTime(" + JSON.stringify(user.username) + ")'>迟到</label>" +
                "<label><input type='radio' name='state_" + user.username + "' value='leave' onclick='hideLaterTime(" + JSON.stringify(user.username) + ")'>请假</label>" +
                "<label><input type='radio' name='state_" + user.username + "' value='truancy' onclick='hideLaterTime(" + JSON.stringify(user.username) + ")'>旷课</label>&nbsp;" +
                "<label id='late_" + user.username + "' hidden='hidden'>迟到时间：<input class='late_time' type='datetime-local'></label></label></div>");
        }
    });
    if ($(".modal-content").html()=="")
        $(".modal-content").html("今日全部成员都已签到");
}

function addSignRecord() {
    let team_member = JSON.parse(sessionStorage.getItem("team_member"));
    let user_name = new Array();
    let state = new Array();
    let late_time = new Array();
    $(".user_name>input").each(function (key, value) {
        user_name[key] = $(this).val();
    });
    $(team_member).each(function (key, value) {
        state[key] = $("input[name='state_" + value.username + "']:checked").val();
    });
    $(".late_time").each(function (key, val) {
        late_time[key] = $(this).val();
    })
    console.log(user_name);
    console.log(state);
    console.log(late_time);
    let map = {
        "user_name": JSON.stringify(user_name),
        "status": JSON.stringify(state),
        "late_time": JSON.stringify(late_time)
    }
    let data = myAjax("/sign/addSignRecords", "post", map, "json")
    if (data == 200) {
        alert("点到完成");
        let mask = $(".mask");
        let modal = $(".modal");
        mask.css("display", "none");
        modal.css("display", "none");
    } else if (data == 500)
        alert("提交出错");
    else
        alert("404未找到");
    setSignInData();
}

//隐藏迟到时间
function hideLaterTime(username) {
    //如果没有hidden属性，则添加该属性
    if ($("#late_" + username).attr("hidden") == undefined)
        $("#late_" + username).attr("hidden", "hidden");
    $("#late_" + username + ">input").val("");
}

//显示迟到时间
function showLaterTime(username) {
    //如果有hidden属性，则去掉该属性
    if ($("#late_" + username).attr("hidden") != undefined)
        $("#late_" + username).removeAttr("hidden");
}

//避免输出空值
function avoidNull(data) {
    if (data == null)
        data = "待完善";
    return data;
}

function getSignByUserAndSignTime(username) {
    console.log(JSON.parse(username));
    let data = myAjax("/sign/getSignByUserAndSignTime", "get",{"username":JSON.parse(username)},"json");
    console.log(data);
    return data;
}